<?php
namespace Admin\Controller;
class OrderController extends CommonController {
    
    /*
     *
     * 列表
     *
     */
    public function index(){
        
        $act=I('get.act');
        
        if (!empty($act)&&$act==='search') {
            $keywords=trim(urldecode(I('keywords')));
            $condition=array();
            $con=array();
            if(!empty($keywords)){
                $condition['realname']=array('like','%'.$keywords.'%');
                
            }
            $con['keywords']=$keywords;
        }
        
        
        $count=M('goods_rec r')->join('left join '.C('DB_PREFIX').'user u on u.uid=r.uid')->where($condition)->count();
        
        $p=getpage($count,9);
        
        foreach($con as $key=>$val)
        {
            $p->parameter[$key]  = urlencode($val);
        }
        
        $goods_arr=M('goods_rec r')->join('left join '.C('DB_PREFIX').'user u on u.uid=r.uid')->where($condition)->order('grid desc ')->limit($p->firstRow.','.$p->listRows)->select();
        
    
        $this->goods=$goods_arr;
        
        $this->assign('page',$p->show());
        
        $this->assign('con',$con);
        
        $this->display();
        
    }
    
    /*
     *
     *  添加+修改
     *
     *
     */
    public function add(){
        
        $act=I('act','');
        
        $id=I('id',0,'intval');
        
        if($act=='add')
        {
            
            $is_get=I('post.is_get');
           
            $db_arr=array(
                'is_get'=>$is_get,
            
            );
            
           
            if(!empty($id)){
            
                $lastId=M('goods_rec')->where(array('grid'=>$id))->save($db_arr);
                
                
            } 
            
            !empty($lastId)?message(1,L('_DO_SUCCEED_'),U('Order/index')):message(2,L('_DO_FAILED_'));
            
        }
        
        $this->edit=!empty($id)?M('goods_rec')->where(array('grid'=>$id))->find():'';
      
        $this->display();
    }
    
    /*
     *
     *  删除
     */
    public function del(){
        
        
        $id=I('post.id',0,'intval');
        
        if(!empty($id)){
            
            $id_arr=is_array($id)?$id:array($id);
            
            foreach($id_arr as $k=>$v){
                
                $arr=M('goods_rec')->where(array('grid'=>$v))->find();
                
                !empty($arr['grpics'])?unlink($arr['grpics']):'';//删除缩略图
              
                $delId=M('goods_rec')->where(array('grid'=>$v))->delete();
                
                if(empty($delId)) continue;
                
            }
            
            message(1,L('_DO_SUCCEED_'),U('Order/index'));
        }
        message(2,L('_DO_FAILED_'));
        
        
    }

    //批量修改领取状态
    public function status_ajax()
    {
        if(isset($_POST)){
            $ids = I('ids');
            $status = I('status');
            // return $this->ajaxReturn(['code'=>0,'data'=>$ids,'msg'=>$status]);
            if(empty($ids)) $this->ajaxReturn(['code'=>0,'data'=>'','msg'=>'参数错误']);
            $where['grid']=array('in',$ids);
            $re = M('goods_rec')->where($where)->save([
                'is_get'=>$status
            ]);
            if(!$re) $this->ajaxReturn(['code'=>0,'data'=>'','msg'=>'更新失败']);

            $this->ajaxReturn(['code'=>200,'data'=>'','msg'=>'ok']);

        }else{
            $this->ajaxReturn(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    
    
}

